package com.ghovos.usermangerpgdemo.contorller.api;

import com.ghovos.usermangerpgdemo.common.result.GlobalResult;
import com.ghovos.usermangerpgdemo.entity.MenuEntity;
import com.ghovos.usermangerpgdemo.service.MenuService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author liuli
 */
@RestController
@RequestMapping("/menu")
@AllArgsConstructor
@Api(tags = "菜单管理api")
public class MenuController {

    private final MenuService menuService;

    /**
     * 查出所有menu 包括额外信息
     * @return menu集合
     */
    @ApiOperation(("查出所有menu 包括额外权限"))
    @GetMapping("/all/ext")
    public GlobalResult<List<MenuEntity>> allExt(){
        return GlobalResult.ok(menuService.getAllMenuExt());
    }

    /**
     * 通过id查询  包括额外权限信息
     * @param id menuId
     * @return menu
     */
    @ApiOperation("通过id查询  包括额外信息")
    @GetMapping("/find/ext/{id}")
    public GlobalResult<MenuEntity> getExtById(@PathVariable("id") Long id){
        return GlobalResult.ok(menuService.getMenuExtById(id));
    }

    /**
     * 添加或更新菜单
     * @param menuEntity menuEntity
     * @return 添加成功后的实体
     */
    @ApiOperation("添加或更新菜单")
    @PostMapping(value = {"/update", "/add"})
    public GlobalResult<Object> addOrSave(@RequestBody MenuEntity menuEntity){
        menuService.saveOrUpdate(menuEntity);
        return GlobalResult.ok(menuEntity);
    }


    /**
     * 根据 id 删除 菜单
     * @param id 菜单id
     * @return 删除是否成功
     */
    @ApiOperation("根据id删除菜单")
    @GetMapping("/delete/{id}")
    public GlobalResult<Object> deleteById(@PathVariable("id") Long id){
        return GlobalResult.ok(menuService.deleteByIdWithRole(id));
    }
}
